package cn.ticket.controller;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ticket.model.Newsblackboard;
import cn.ticket.service.NewsBlackBoardService;
import cn.ticket.service.Utility;

@Controller
@RequestMapping("newsBlackBoard")
public class NewsBlackBoardController {
	@Autowired
	NewsBlackBoardService newsBlackBoardService;
	
	@RequestMapping(value="insert",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String insert(@RequestBody Map<String, Object> record) throws ParseException{
		Map<String, Object> result=new HashMap<>();
		Newsblackboard newsblackboard=new Newsblackboard();
		Integer staffid=Integer.parseInt((String)record.get("staffid"));
		newsblackboard.setStaffid(staffid);
		newsblackboard.setTopic((String) record.get("topic"));
		newsblackboard.setContent((String) record.get("content"));
		newsblackboard.setExpiredate((long) record.get("expiredate"));
		newsblackboard.setValue((byte)((Integer)record.get("value")).intValue());
		int num = newsBlackBoardService.insertSelective(newsblackboard);
		if (num==1) {
			result.put("status", 1001);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="update",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String update(@RequestBody Newsblackboard record){
		Map<String, Object> result=new HashMap<>();
		int num = newsBlackBoardService.updateByPrimaryKeySelective(record);
		if (num==1) {
			result.put("status", 1001);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="delete",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String delete(@RequestBody Map<String, Integer> args){
		Map<String, Object> result=new HashMap<>();
		Integer id=args.get("newsId");
		int staffId=args.get("staffId");
		Newsblackboard newsblackboard=newsBlackBoardService.selectByPrimaryKey(id);
		if (newsblackboard.getStaffid()!=staffId) {
			result.put("status", 1002);
			result.put("message", "非消息发布者!");
			return Utility.gson.toJson(result);
		}
		int num = newsBlackBoardService.deleteByPrimaryKey(id);
		if (num==1) {
			result.put("status", 1001);
		}
		else {
			result.put("status", 1002);
			result.put("message", "删除失败!");
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="getValidateNews",produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getValidateNews(){
	//	Map<String, Object> result=new HashMap<>();
		List<Map<String, Object>> news=newsBlackBoardService.getValidateNews();
		return Utility.createJsonMsg(1001, "success", news);
	}
}
